package com.lhj.xxsc.ware.listener;

import com.xxsc.common.to.mq.OrderTo;
import com.xxsc.common.to.mq.StockLockedTo;
import com.lhj.xxsc.ware.feign.OrderFeignService;
import com.lhj.xxsc.ware.service.WareOrderTaskDetailService;
import com.lhj.xxsc.ware.service.WareOrderTaskService;
import com.lhj.xxsc.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/7 16:02
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
	@Autowired
	WareOrderTaskDetailService wareOrderTaskDetailService;
	@Autowired
	WareOrderTaskService wareOrderTaskService;
	@Autowired
	OrderFeignService orderFeignService;
	@Autowired
	WareSkuService wareSkuService;

	@RabbitHandler
	public void handleStockLockedRelease(StockLockedTo to, Message message, Channel channel) throws IOException {

		System.out.println("收到解锁消息");
		try {
			wareSkuService.unLockStock(to);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
		}

	}
	@RabbitHandler
	public void handleOrderCloseRelease(OrderTo orderTo, Message message, Channel channel) throws IOException {
		System.out.println("订单已经关闭，准备解锁库存");
		try {
			wareSkuService.unLockStock(orderTo);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
		}
	}
}

